package com.aaa.service.impl.sas;

import com.aaa.entity.sas.SasPlan;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.mapper.sas.SasMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.sas.TQuestionSas;
import com.aaa.service.sas.SasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;


@Service
public class SasServicelmpl implements SasService {
    @Autowired
    private SasMapper tQuestionSasMapper;

    @Override
    public PageInfo queryAll (MyPage myPage, TQuestionSas tQuestionSas){


        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
         List<TQuestionSas> tQuestionSass = tQuestionSasMapper.findAllQuestions(tQuestionSas);
        PageInfo pageInfo = new PageInfo(tQuestionSass);

        return pageInfo;

    }
    @Override
    public boolean insertQuestion(TQuestionSas tQuestionSas){
        int insert = -1;
        if (tQuestionSas.getId() == null){
            insert = tQuestionSasMapper.insert(tQuestionSas);
        }else {
            insert = tQuestionSasMapper.updateByPrimaryKeySelective(tQuestionSas);
        }
        return insert>0?true:false;
    }
    @Override
    public boolean queryQuestion(String question) {
        Example example = new Example(TQuestionSas.class);
        example.createCriteria().andEqualTo("question",question);
        TQuestionSas tQuestionSas = tQuestionSasMapper.selectOneByExample(example);
        System.out.println("tQuestionSas = " + tQuestionSas);
        if (tQuestionSas==null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteQuestion(Integer id) {
        int delete = tQuestionSasMapper.deleteByPrimaryKey(id);
        return delete>0?true:false;
    }

    @Override
    public boolean uploadExcel(MultipartFile file) {

        ExcelReaderBuilder read = null;
        try {
            read = EasyExcel.read(file.getInputStream());
            List<LinkedHashMap<String,Object>> objects = read.doReadAllSync();
            for (LinkedHashMap<String, Object> object : objects) {
                TQuestionSas tQuestionSas = new TQuestionSas();
                tQuestionSas.setQuestion((String) object.get(0));
                tQuestionSas.setOptiona((String) object.get(1));
                tQuestionSas.setOptionb((String) object.get(2));
                tQuestionSas.setOptionb((String) object.get(3));
                tQuestionSas.setOptionb((String) object.get(4));
//               personInfoDTO.setNickname((String) object.get(0));
                System.out.println("tQuestionSas = " + tQuestionSas);
                int i = tQuestionSasMapper.loginAddInfo(tQuestionSas);
                if(i<=0 ){
                    return false;
                }
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }











}
